package com.boruan.qq.zxgylibrary.ui.activities;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.R2;
import com.boruan.qq.zxgylibrary.base.BaseActivity;
import com.boruan.qq.zxgylibrary.constants.ConstantInfo;
import com.boruan.qq.zxgylibrary.utils.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 崔琦 on 2019/8/19.
 * des: 修改订单信息页面
 */

public class ModifyOrderInfoActivity extends BaseActivity {
    @BindView(R2.id.ll_add_web)
    LinearLayout llAddWeb;

    private CustomDialog dialog;

    @Override
    protected int getLayoutId() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        return R.layout.drug_activity_modify_order_info;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        dialog = new CustomDialog(this, R.style.CustomDialog);
        dialog.show();
        initCommonWeb(ConstantInfo.baseUrl + "change?type=android&token=" + ConstantInfo.h5UserToken);
    }

    /**
     * 初始化web
     */
    @SuppressLint("JavascriptInterface")
    private void initCommonWeb(final String richText){
        commonWeb.loadUrl(richText);
        commonWeb.addJavascriptInterface(ModifyOrderInfoActivity.this, "control");
        commonWeb.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dialog.dismiss();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //dialog.show();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();//接受所有网站的证书，忽略SSL错误
                
            }

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(view, webResourceRequest, webResourceError);
                if (webResourceRequest.isForMainFrame()) {
//					view.loadUrl("about:blank");// 避免出现默认的错误界面
//                    Toast.makeText(ModifyOrderInfoActivity.this, "网络加载异常，请稍后重试！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    return;
                }
//				view.loadUrl("about:blank");// 避免出现默认的错误界面
//                Toast.makeText(ModifyOrderInfoActivity.this, "网络加载异常，请稍后重试！", Toast.LENGTH_SHORT).show();
            }
			
			@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                // 这个方法在 android 6.0才出现
                int statusCode = errorResponse.getStatusCode();
                if (404 == statusCode || 500 == statusCode) {
//                    view.loadUrl("about:blank");// 避免出现默认的错误界面
                }
            }
        });
        llAddWeb.addView(commonWeb);
    }

    @OnClick(R2.id.tv_close_shop)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        Log.i("测试","ondes");
        if (commonWeb != null) {
            //加载null内容
            commonWeb.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            //清除历史记录
            commonWeb.clearHistory();
            //移除WebView
            ((ViewGroup) commonWeb.getParent()).removeView(commonWeb);
            //销毁VebView
            commonWeb.destroy();
            //WebView置为null
            commonWeb = null;
        }
        super.onDestroy();
    }
}
